package com.example.gty.service.Impl;


import com.example.gty.entity.RolePremission;
import com.example.gty.entity.RolePremissionVo;
import com.example.gty.mapper.AdminsMapper;
import com.example.gty.mapper.RolePremissionMapper;
import com.example.gty.service.AdminsService;
import com.example.gty.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminsServiceImpl implements AdminsService {
    @Autowired
    private RolePremissionMapper rolePremissionMapper;
    @Override
    public List<RolePremissionVo> selectAllPer() {
        List<RolePremissionVo> rolePremissionVos = rolePremissionMapper.selectAllPer();
        return rolePremissionVos;
    }

    //id 查询权限
    public List<RolePremissionVo> selectAllPerById(String no) {
        return rolePremissionMapper.selectAllPerById(no);
    }

    //权限管理
    public ResultVo permissionConntroller(String no, String pers) {

        Example example = new Example(RolePremission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("role",no);
        int i = rolePremissionMapper.deleteByExample(example);

        //添加权限
        String[] split = pers.split(",");
        int k=0;
        for (int j = 0; j < split.length; j++) {
            RolePremission rolePremission = new RolePremission(no, split[j]);
           k= rolePremissionMapper.insert(rolePremission);
        }
        if (k>0){
            System.err.println("success");
            return new ResultVo(1000,"success",null);
        }else {
            System.err.println("err");
            return new ResultVo(1001,"success",null);
        }

    }

    //id查询权限

}
